using System;
using System.Text;
using System.Reflection;
using System.Collections.Generic;

namespace Open.Data.Persistence
{
    //Abstract representation of the class involved in encding and decoding property valeus from storage data objects
    public abstract class Formatter
    {
        //Property variable
        private QueryDataAdapter _adapter;

        //Properties
        public virtual QueryDataAdapter Adapter
        {
            get
            {
                return _adapter;
            }
            set
            {
                _adapter = value;
            }
        }

        public abstract Command Command { get; set;}
        public abstract Concurrency Concurrency { get; set;}

        //Methods
        public abstract void Encode(IPersistable graph, Type type);
        public abstract void Decode(IPersistable graph, Type type);

        public virtual IPersistable ActivateObject(Type type, object key)
        {
            IPersistable persistable = null;
            TypeInfo typeInfo = null;

            //Get an object from the adapter if it exists otherwise return the object from the adapter
            if (Adapter != null && Adapter.Types != null)
            {
                //Try get a typeinfo object for this type
                Adapter.Types.TryGetValue(type, out typeInfo);

                if (typeInfo != null) typeInfo.Objects.TryGetValue(key, out persistable);

                if (persistable != null) return persistable;
            }

            //If no adapter, no type or no object found, then create
            if (persistable == null)
            {
                persistable = Command.Context.CreatePersistable(type);

                //Set up concurrency
                if (persistable is IConcurrency) (persistable as IConcurrency).SetConcurrency(Command.Context.CreateConcurrency());
            }

            //Add back to adapter
            if (Adapter != null && Adapter.Types != null)
            {
                if (typeInfo == null)
                {
                    typeInfo = new TypeInfo();
                    typeInfo.Type = type;
                    typeInfo.Objects = new Dictionary<object, IPersistable>();

                    Adapter.Types.Add(type, typeInfo);
                }

                typeInfo.Objects.Add(key, persistable);
            }
            
            return persistable;
        }

        public abstract void Reset();

        public static string GetPropertyName(PropertyInfo prop, string seperator)
        {
            string propname = prop.Name;

            //Add the key name to the property name if an IPersistable object
            if (typeof(IPersistable).IsAssignableFrom(prop.PropertyType))
            {
                CacheItem propcache = Provider.Instance.ReflectionCache.Lookup(prop.PropertyType);
                propname += seperator + GetPropertyName(propcache.PrimaryKey, "");
            }
            return propname;
        }
    }
}
